package com.xfcy.blog.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xfcy.blog.common.R;
import com.xfcy.blog.common.constant.Constant;
import com.xfcy.blog.common.constant.SqlConstant;
import com.xfcy.blog.common.enums.ResultCode;
import com.xfcy.blog.dto.MessageDTO;
import com.xfcy.blog.mapper.MessageMapper;
import com.xfcy.blog.entity.Message;
import com.xfcy.blog.service.MessageService;
import com.xfcy.blog.utils.BeanCopyUtil;
import com.xfcy.blog.utils.IpUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (Message)表服务实现类
 *
 * @author 晓风残月Lx
 * @since 2023-04-09 11:03:05
 */
@Service("messageService")
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    private HttpServletRequest request;

    /**
     * 后台留言列表
     * @param pageNo
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public R listMessage(Integer pageNo, Integer pageSize, String name) {
        Page<Message> list = baseMapper.selectPage(new Page<>(pageNo, pageSize), new QueryWrapper<Message>().lambda()
                .like(StringUtils.isNotBlank(name), Message::getNickname, name)
                .orderByDesc(Message::getCreateTime));
        return R.success(list);
    }

    /**
     * 批量删除留言
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteBatchMessage(List<Integer> ids) {
        baseMapper.deleteBatchIds(ids);
        return R.success();
    }

    /**
     * 批量通过
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R passBatch(List<Integer> ids) {
        Assert.notEmpty(ids, ResultCode.VAILID_EXCEPTION.getDesc());
        baseMapper.passBatch(ids);
        return R.success();
    }


    // WEB方法=======================
    /**
     * 留言列表
     * @return
     */
    @Override
    public R webMessage() {
        // 查询留言列表
        List<Message> messageList = baseMapper.selectList(new LambdaQueryWrapper<Message>()
                .select(Message::getId, Message::getNickname, Message::getAvatar,
                        Message::getContent, Message::getTime).eq(Message::getStatus, Constant.ONE));
        return R.success(messageList);
    }

    /**
     * 添加留言
     * @param messageDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addMessage(MessageDTO messageDTO) {
        Message message = BeanCopyUtil.copyObject(messageDTO, Message.class);
        // 获取IP地址
        String ipAddress = IpUtils.getIp(request);
        // 获取IP属地
        String ipSource = IpUtils.getCityInfo(ipAddress);
        message.setIpAddress(ipAddress);
        message.setIpSource(ipSource);
        baseMapper.insert(message);
        return R.success("留言成功");
    }
}


